import 'dart:math';

import 'package:bruno/bruno.dart';
import 'package:dio/dio.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_appgetwe/page/modelutils/video_list.dart';
import 'package:flutter_appgetwe/wiget/video_player_item.dart';
import 'package:like_button/like_button.dart';
import 'dart:math' as math;

class VideoScreen extends StatefulWidget {
  @override
  _VideoScreenState createState() => _VideoScreenState();
}

class _VideoScreenState extends State<VideoScreen>
    with TickerProviderStateMixin {
  //旋转动画
  late AnimationController _controller;
  bool isAns = true;
  //_controller.dispose();
  //_controller.forward();
  //动画结束

  bool isLike = false;

  bool isfollow = true;

  bool isPlay = true;

  //List<Map> mapList = [];
  List<VideoList?> mapList = [];
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _controller = AnimationController(
      duration: const Duration(seconds: 10),
      vsync: this,
    )..repeat();

    for (int i = 0; i < 20; i++) {
      // mapList.add({
      //   "videoUrl": "http://rec6p45ah.hn-bkt.clouddn.com/1" +
      //       (Random.secure().nextInt(30) % 10 != 1 ? 6 : 7).toString() +
      //       ".mp4",
      //   "id": i,
      //   "like": 500 + i,
      //   "common": 20 + i,
      //   "fellow": 50 + i,
      // });
    }
    ;
    _get();
  }

  void _get() async {
    print("get请求");
    var result = await Dio().post(
        "http://192.168.1.103:18063/tkVideo/api/hch/tkVideo/find/all/1/10");

    List<VideoList?> list = (result.data as List<dynamic>)
        .map((e) => VideoList.fromMap((e as Map<String, dynamic>)))
        .toList();
    setState(() {
      mapList = list;
    });

    print("打印单个");
    print(mapList[1]!.videoUrl.toString());
  }

  //bool isLiked = false;

  Future<bool> onLikeButtonTapped(bool isLiked) async {
    // setState(() {
    //   isLiked = !isLiked;
    // });

    return !isLiked;
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    double widthSize = MediaQuery.of(context).size.width as double; // 360.0
    double heightSize = MediaQuery.of(context).size.height as double; // 640.0
    print(widthSize.toString());
    print(heightSize.toString());
    return Scaffold(
      body: PageView.builder(
          itemCount: mapList.length == 0 ? 10 : mapList.length,
          controller: PageController(initialPage: 0, viewportFraction: 1),
          scrollDirection: Axis.vertical,
          itemBuilder: (context, index) {
            if (index > mapList.length - 5) {
              _showVideoList();
            }
            return Stack(
              children: [
                GestureDetector(
                  onTap: () {
                    print(".....暂停");
                    _controller.dispose();
                  },
                  onDoubleTap: () {
                    print("双击了");

                    onLikeButtonTapped(true);
                  },
                  child: VideoPlayerItem(
                      videoUrl: mapList.length > 0
                          ? mapList[index]!.videoUrl.toString()
                          : "http://rec6p45ah.hn-bkt.clouddn.com/9.mp4",
                      isPlayFunction: (e) {
                        print("接收到的参数是");
                        print(e);
                        if (e) {
                          _controller.forward();
                          print(_controller.status);
                        } else {
                          _controller.reset();
                        }
                      },
                      isPlay: true),
                ),
                Positioned(
                  bottom: 40,
                  left: 8,
                  child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        SizedBox(
                          height: 3,
                        ),
                        Container(
                          child: Text(
                            "rivaanranawat",
                            style: TextStyle(
                                fontSize: 20, fontWeight: FontWeight.w600),
                          ),
                        ),
                        SizedBox(
                          height: 3,
                        ),
                        Container(
                          child: Text(
                            "No Song, Only Tript",
                            style: TextStyle(fontSize: 16),
                          ),
                        ),
                        SizedBox(
                          height: 3,
                        ),
                        Stack(
                          //mainAxisAlignment: MainAxisAlignment.start,
                          children: [
                            Container(
                              width: 180,
                              height: 20,
                              //color: Colors.deepPurpleAccent,
                            ),
                            //Icon(IconData(0xe8b2,fontFamily: 'MyIcons'),color: Colors.white,size: 20,),
                            Positioned(
                                left: -22,
                                child: Padding(
                                  padding: EdgeInsets.only(left: 1, right: 1),
                                  child: Container(
                                    width: 210,
                                    height: 18,
                                    //color: Colors.orange,
                                    child: BrnNoticeBar(
                                      content:
                                          '这是跑马灯的通知内容跑马灯的通知内容跑马灯的通知内容跑马灯的通知内容',
                                      marquee: true,
                                      textColor: Colors.white,
                                      backgroundColor:
                                          Color.fromRGBO(222, 222, 222, 0),
                                      showRightIcon: false,
                                      //showLeftIcon: false,
                                      leftWidget: Icon(
                                        IconData(0xe8b2, fontFamily: 'MyIcons'),
                                        color: Colors.white,
                                        size: 20,
                                      ),
                                      noticeStyle:
                                          NoticeStyles.runningWithArrow,
                                      onNoticeTap: () {
                                        //BrnToast.show('点击通知', context);
                                      },
                                      onRightIconTap: () {
                                        //BrnToast.show('点击右侧图标', context);
                                      },
                                    ),
                                  ),
                                )),
                          ],
                        )
                      ]),
                ),
                Positioned(
                    right: 10,
                    bottom: 31,
                    child: Column(
                      children: [
                        Stack(
                          alignment: Alignment.center,
                          children: [
                            Container(
                              height: 55,
                              width: 55,
                              //color: Colors.blue,
                            ),
                            Container(
                              width: 40,
                              height: 40,
                              decoration: BoxDecoration(
                                  borderRadius: BorderRadius.circular(40),
                                  image: DecorationImage(
                                    image: NetworkImage(
                                        "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fc-ssl.duitang.com%2Fuploads%2Fitem%2F202105%2F29%2F20210529001057_aSeLB.thumb.1000_0.jpeg&refer=http%3A%2F%2Fc-ssl.duitang.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1659189274&t=79ca0cb6236830ee1b7f343efc6f5824"),
                                    // fit: BoxFit.fill,
                                    colorFilter: ColorFilter.mode(
                                        Colors.black.withOpacity(0.2),
                                        BlendMode.darken),
                                  )),
                            ),
                            Positioned(
                              bottom: -1,
                              width: 20,
                              height: 16,
                              child: InkWell(
                                onTap: () {
                                  print("dianji..");
                                  setState(() {
                                    isfollow = !isfollow;
                                  });
                                },
                                child: isfollow
                                    ? Container(
                                        width: 20,
                                        height: 20,
                                        alignment: Alignment.center,
                                        decoration: BoxDecoration(
                                            borderRadius:
                                                BorderRadius.circular(20),
                                            color: Colors.red[300]),
                                      )
                                    : Text(""),
                              ),
                            ),
                            Positioned(
                              bottom: -3,
                              left: 22,
                              child: Padding(
                                padding: EdgeInsets.only(
                                  top: 20,
                                ),
                                child: Container(
                                  child: Text(
                                    isfollow ? "+" : "",
                                    style: TextStyle(fontSize: 18),
                                  ),
                                ),
                              ),
                            )
                          ],
                        ),
                        SizedBox(
                          height: 10,
                        ),
                        Column(
                          crossAxisAlignment: CrossAxisAlignment.center,
                          mainAxisAlignment: MainAxisAlignment.center,
                          children: [
                            InkWell(
                              onTap: () {
                                print("喜欢");
                                setState(() {
                                  isLike = !isLike;
                                });
                              },
                              child: Container(
                                margin: EdgeInsets.only(bottom: 3),
                                alignment: Alignment.center,
                                child: LikeButton(
                                  onTap: onLikeButtonTapped,
                                  size: 30,
                                  circleColor: CircleColor(
                                      start: Color.fromRGBO(247, 104, 50, 1),
                                      end: Color.fromRGBO(249, 68, 108, 1)),
                                  bubblesColor: BubblesColor(
                                    dotPrimaryColor:
                                        Color.fromRGBO(247, 104, 50, 1),
                                    dotSecondaryColor:
                                        Color.fromRGBO(249, 68, 108, 1),
                                  ),
                                  likeBuilder: (bool isLiked) {
                                    return Icon(
                                      Icons.favorite,
                                      color:
                                          isLiked ? Colors.red : Colors.white,
                                      size: 35,
                                    );
                                  },
                                  //likeCount: 665,
                                ),
                              ),
                            ),
                            Text("688"),
                          ],
                        ),
                        SizedBox(
                          height: 20,
                        ),
                        Column(
                          children: [
                            InkWell(
                                onTap: () {
                                  print("评论");
                                },
                                child: Container(
                                  margin: EdgeInsets.only(bottom: 3),
                                  child: Icon(
                                    IconData(0xe7f6, fontFamily: 'MyIcons'),
                                    color: Colors.white,
                                    size: 30,
                                  ),
                                )),
                            Text("9775"),
                          ],
                        ),
                        SizedBox(
                          height: 20,
                        ),
                        Column(
                          children: [
                            InkWell(
                                onTap: () {
                                  print("收藏");
                                },
                                child: Container(
                                  margin: EdgeInsets.only(bottom: 3),
                                  child: Icon(
                                    IconData(0xeca6, fontFamily: 'MyIcons'),
                                    color: Colors.white,
                                    size: 30,
                                  ),
                                )),
                            Text("685"),
                          ],
                        ),
                        SizedBox(
                          height: 20,
                        ),
                        Column(
                          children: [
                            InkWell(
                                onTap: () {
                                  print("分享");
                                },
                                child: Container(
                                  margin: EdgeInsets.only(bottom: 3),
                                  child: Icon(
                                    IconData(0xe78e, fontFamily: 'MyIcons'),
                                    color: Colors.white,
                                    size: 25,
                                  ),
                                )),
                            Text("456"),
                          ],
                        ),
                        SizedBox(
                          height: 20,
                        ),
                        Stack(
                          children: [
                            Container(
                              width: 50,
                              height: 50,
                              //color: Colors.deepPurpleAccent,
                            ),
                            Positioned(
                              top: -10,
                              left: 0,
                              child: AnimatedBuilder(
                                animation: _controller,
                                child: Padding(
                                  child: Container(
                                      width: 35.0,
                                      height: 35.0,
                                      //alignment: Alignment.center,
                                      child: Padding(
                                        padding: EdgeInsets.all(2),
                                        child: Container(
                                          width: 27,
                                          height: 27,
                                          alignment: Alignment.center,
                                          decoration: BoxDecoration(
                                            border: Border.all(
                                                width: 7,
                                                color: Colors.black26),
                                            borderRadius:
                                                BorderRadius.circular(200),
                                            color:
                                                Color.fromRGBO(60, 63, 65, 1),
                                            // image: DecorationImage(
                                            //     image: NetworkImage(
                                            //         "http://rec6p45ah.hn-bkt.clouddn.com/666.jpg"),
                                            //     fit: BoxFit.fill),
                                          ),
                                          child: Container(
                                            decoration: BoxDecoration(
                                              border: Border.all(
                                                  width: 10,
                                                  color: Colors.black26),
                                              borderRadius:
                                                  BorderRadius.circular(30),
                                              //color: Colors.orange,
                                              image: DecorationImage(
                                                  image: NetworkImage(
                                                      "http://rec6p45ah.hn-bkt.clouddn.com/666.jpg"),
                                                  fit: BoxFit.fill),
                                            ),
                                          ),
                                        ),
                                      )),
                                  padding: EdgeInsets.only(
                                      left: 10, right: 10, top: 10, bottom: 10),
                                ),
                                builder: (BuildContext context, Widget? child) {
                                  return Transform.rotate(
                                    angle: _controller.value * 2.0 * math.pi,
                                    child: child,
                                  );
                                },
                              ),
                            ),
                          ],
                        )
                      ],
                    )),
              ],
            );
          }),
    );
  }

  void _showVideoList() {
    // for (int i = 0; i < 20; i++) {
    //   mapList.add({
    //     "videoUrl": "http://rec6p45ah.hn-bkt.clouddn.com/1" +
    //         (Random.secure().nextInt(30) % 10 == 1 ? 6 : 7).toString() +
    //         ".mp4",
    //     "id": i,
    //     "like": 500 + i,
    //     "common": 20 + i,
    //     "fellow": 50 + i,
    //   });
    // }
  }
}
